07. Quiz: Detect Lane Lines

Line Detection and Navigation

Now it's your turn to apply what you know about the Hough transform on a different image. The Hough transform is often used in simple navigation techniques to find and align vehicles with lane lines or other well-defined paths.

So, in this example, you'll use the Hough transform to detect lane lines in an image of a road.

Road marked with lane lines

Road marked with lane lines

The starter code will result in too many lines detected, so in this quiz, it's up to you to modify the parameters of the Hough transform so that the lane lines are detected and not any lines in the sky or scenery.

You should aim for line detection that looks like the image below.

Tip: You can filter lines by calculating their slope, m, (m = change in y / change in x) and only drawing lines on an image that fall in a certain range of slope values. Also, you can change the values for the Canny threshold but the provided values should work fairly well.

Line detected image

Line detected image

Start Quiz:

import numpy as np
import matplotlib.pyplot as plt
import cv2

# Read in an image and convert to RGB
image = cv2.imread('road_1.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Peform edge detection with Canny
low_threshold = 90
high_threshold = 180
edges = cv2.Canny(gray, low_threshold, high_threshold)
# ---------------------------------------------------------- #


## TODO: Define the Hough transform parameters 
## so that lane lines are the only ones detected
rho = 1
theta = np.pi/180
threshold = 50
min_line_length = 30
max_line_gap = 8


line_image = np.copy(image) #creating an image copy to draw lines on

# Run Hough on the edge-detected image
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                        min_line_length, max_line_gap)


# Iterate over the output "lines" and draw lines on the image copy
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),3)

# ---------------------------------------------------------- #
plt.imshow(line_image)